package Wallpaper::SanityCheck;
$VERSION = sprintf "%.3f", 1 + substr(q$Revision: 166 $,9)/1000;

use Storable;

use Wallpaper::Configuration qw( %datastore );
use Wallpaper::Randomizer qw( randomfile );
use Wallpaper::Tools qw( timedata getfiletime getdir );

my $general_config = Wallpaper::Configuration->general;
my $time           = Wallpaper::Tools->timedata( localtime );
my $bgdir          = $general_config->{ 'bgdir' };
my $alldisplay     = Wallpaper::Configuration->all_display;

# Where to store state info and wallpaper output
if ( !-d "/dev/shm/walls" ) {
    system( "mkdir /dev/shm/walls" );
    unlink( "/tmp/wallpaper.dump" );  # If there is no walls dir, this is a reboot, do not load old config!
}

# Check for a conky tiled background image
if ( !-e $bgdir . '/backtile.jpg' ) {
    die "No conky background tile photo found!  Download a tiling photo and save it as $bgdir/backtile.jpg\n";
}

# Check for blank working image
if ( !-e $bgdir . '/blank.png' ) {
    die "No blank photo file found!, create a 1x1 blank photo and save it as $bgdir/blank.png\n";
}

# Read old config
if ( -e '/tmp/wallpaper.dump' ) {
    my $storable = retrieve('/tmp/wallpaper.dump');

    if ( $storable ) {
        %datastore = %{ $storable };
        undef $storable;
        unlink( '/tmp/wallpaper.dump' ); # Remove old dump.

    } else {
    &init;
    }

} else {
    &init;
}

sub init {

    # No existing config, so start from scratch.
    # Prepopulate datastore
    my @items = qw( 0 1 2 3 4 );
    for my $primary ( @items ) {
        for my $secondary ( @items ) {
            my $display = $primary . "." . $secondary;
            if ( $alldisplay->{ $display }{ 'enable' } ) {
                my $display = $primary . "." . $secondary;

                my $photodir = $general_config->{ 'bgdir' } . "/" . $general_config->{ 'photoset' }[ $time->{ 'hour' } ];

                my @toselect1 = getdir( $photodir );                    # Get a list of files to choose from
                my $selected1 = randomfile( \@toselect1, $display, $photodir );    # Pick a new image for upcoming photo
                my $current   = $photodir . "/" . $selected1;           # Full path to new image
                $datastore{ $display }{ 'current' } = $current;
                my @toselect2 = getdir( $photodir );                    # Get a list of files to choose from
                my $selected2 = randomfile( \@toselect2, $display, $photodir );    # Pick a new image for upcoming photo
                my $next      = $photodir . "/" . $selected2;           # Full path to new image
                $datastore{ $display }{ 'next' } = $next;
                $datastore{ 'enabled' }          = 1;                   # Flag for at least 1 display working.
            } ## end if ( $alldisplay->{ $display...
        } ## end for my $secondary ( @items)
    } ## end for my $primary ( @items)

    die( "Please configure at least one display, see .wallrc file for details\n" ) if ( !$datastore{ 'enabled' } );

# Ensure we have enough files for randomization
    for my $index ( qw/1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23/ ) {
        my @files  = getdir( $bgdir . "/" . $general_config->{ 'photoset' }[$index] );
        my $number = scalar @files + 1;                                                  # array item 0 = file # 1 (bah!)
        if ( $number < 7 ) {
            print "You need at least 6 files in $bgdir/$general_config->{'photoset'}[ $index ]!\n";
            die( "FATAL ERROR: Killing script in order prevent a possible race condition\n" );
        }
    } ## end for my $index ( qw/1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23/)
}

1;
